﻿namespace Web.Control.Member.User
{
    using System;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.Text.RegularExpressions;
    using Lx.Web.Common.UI;
    using Lx.Web.Common.ControlsUI;
    using System.Collections;

    using BLF.WWW_User.Users;
    using Model.WWW_User.User;

    public class CLogin : BaseControl
    {
        protected Label lab;
        protected override void OnLoad(EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                lab.Visible = false;
            }
            else
            {
                UserLogin();
            }
            base.OnLoad(e);
        }
        /// <summary>
        /// 登陆
        /// </summary>
        /// <param name="userName">会员号</param>
        /// <param name="pwd">密码</param>
        /// <param name="isSaveCookie">是否保存Cookie</param>
        private void Register(String userName, String pwd, Boolean isSaveCookie)
        {
            string memberNo = (string)User.Login(userName, Globals.Enctype(pwd), Page.Request.UserHostAddress + ":" + Request.ServerVariables["REMOTE_PORT"], isSaveCookie)[0];
                 if (memberNo != "0" && !String.IsNullOrEmpty(memberNo) && memberNo != "-1")
                {

                    HttpCookie authCookie;
                    HttpCookie tagCookie = new HttpCookie("userTag");
                    authCookie = FormsAuthentication.GetAuthCookie(memberNo, isSaveCookie);
                    string cookieName = FormsAuthentication.FormsCookieName;
                    int k = cookieName.IndexOf("$");
                    if (k > 0)
                    {
                        authCookie.Domain = cookieName.Substring(k + 1);
                        tagCookie.Domain = cookieName.Substring(k + 1);
                    }
                    tagCookie.Name = "userTag";
                    tagCookie.Value = Globals.Enctype(pwd);
                    Page.Response.Cookies.Add(authCookie);
                    Page.Response.Cookies.Add(tagCookie);
                    if (Page.Request["ReturnUrl"] != null) 
                    {
                        Page.Response.Redirect(HttpUtility.UrlDecode(Page.Request["ReturnUrl"].ToString()));
                    }
                    else
                    {
                        Response.Write("<script language=javascript>parent.window.location.reload();parent.window.neatDialog.close(); </script>");
                    }
                }
            else
            {
                lab.Text = "Incorrect email or password. ";
                lab.Visible = true;
            }
        }
        protected void UserLogin()
        {
            if (string.IsNullOrEmpty(Request["Username"]) || string.IsNullOrEmpty(Request["UserPassword"]))
            {
                return;
            }
            bool isSaveCookie = false;
            if (!string.IsNullOrEmpty(Request["CheckBox1"]))
            {
                isSaveCookie = true;
            }
            Register(Request["Username"].Trim(), Request["UserPassword"].Trim(), isSaveCookie);
        }
    }
}
